package com.pubinfo.passbook.common.service.master.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRabbitmqPerson;
import com.pubinfo.passbook.common.entity.TRabbitmqPersonConvert;
import com.pubinfo.passbook.common.mapper.TRabbitmqPersonMapper;
import com.pubinfo.passbook.common.model.bo.rabbitmq.RabbitMqRoot;
import com.pubinfo.passbook.common.model.vo.manage.RolePermissionVo;
import com.pubinfo.passbook.common.service.master.TRabbitmqPersonConvertService;
import com.pubinfo.passbook.common.service.master.TRabbitmqPersonService;
import com.pubinfo.passbook.common.service.master.TSystemRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TRabbitmqPersonServiceImpl extends ServiceImpl<TRabbitmqPersonMapper, TRabbitmqPerson>
    implements TRabbitmqPersonService {
  @Resource
  TRabbitmqPersonConvertService personConvertService;
  @Resource
  TSystemRoleService roleService;
  @Transactional

  @Override
  public Boolean insertPersion(JSONObject json) {
    if (json != null) {
      RabbitMqRoot root = JSONObject.toJavaObject(json, RabbitMqRoot.class);
      if ("delete".equals(json.getString("handle"))) {
        Object acctId = JSONPath.eval(json, "$.context.acctId");
        if (acctId != null) {
          removeById(acctId.toString());
          return true;//删除的,先确认
        }
      } else {
        TRabbitmqPerson person = new TRabbitmqPerson(root);
        return saveOrUpdate(person) && personConvertService.processPersonMsg(root); // 转换消息
      }
    }
    return false;
  }
  
  @Override
  public List<TRabbitmqPerson> findDepartmentPersonByAcctId(String acctId) {
    
    return this.baseMapper.findDepartmentPersonByAcctId(acctId);
  }
  
  @Override
  public TRabbitmqPerson selectByAcctId(String accId) {
    return this.baseMapper.selectByAcctId(accId);
  }
  
  @Override
  public Boolean isShengGongSiPerson(String accid) {
    TRabbitmqPerson person = this.baseMapper.findShengGongSiPersonByAcct(accid);
    if (person != null) {
      return true;
    }
    return false;
  }
  
  @Override
  public Boolean isWenZhouPerson(String accid) {
    TRabbitmqPerson person = this.baseMapper.findWenZhouPersonByAcct(accid);
    if (person != null) {
      return true;
    }
    return false;
  }
  
  @Override
  public boolean removePerson(Integer id) {
    if (this.baseMapper.deleteById(id) > 0)
      //同时删除该用户的角色绑定
      return roleService.delUserRole(null, id);
    return false;
  }
  
  @Override
  public String findIdNoByAcct(String acct) {
    TRabbitmqPerson person = selectByAcctId(acct);
    String idNo = null;
    if (person != null) {
      idNo = person.getTbCert().get(0).getCertNo();
    }
    return idNo;
  }
  
  @Override
  public List<RolePermissionVo> getPersonMenu(TRabbitmqPersonConvert current4AUserInfo) {
    List<RolePermissionVo> personMenu = new ArrayList();
    if (current4AUserInfo != null) {
      personMenu = roleService.getPersonMenu(current4AUserInfo.getAcctId());
    }
    if (personMenu.isEmpty()) {
      //用户没权限则默认普通角色
      personMenu = roleService.getRolePermission(2);
    }
    return personMenu;
  }
}




